package com.itzxx.huazhun.utils

import android.os.Handler

class Timer {

    private var handler: Handler = Handler()
    private var callback: (() -> Unit)? = null

    private var mInterval = 0L
    private var  isStop = false

    fun start(interval: Long, callback: () -> Unit) {
        this.callback = callback
        this.mInterval = interval
        handler.postDelayed(timerRunnable, interval)
        isStop = false
    }

    private val timerRunnable = object : Runnable {
        override fun run() {
            if (isStop){
                return
            }
            callback?.invoke()
            handler.postDelayed(this, mInterval)
        }
    }

    fun stop() {
        isStop = true
        handler.removeCallbacks(timerRunnable)
    }
}